package com.guardchina.framework.util.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.Random;

/**
 * 描述：数字工具
 *
 * @author lwb
 * @date 2019/5/24 17:04
 */
public class NumberUtil {

    /**
     * 获取整型 为空则返回0
     *
     * @param value 值
     * @return 值
     */
    public static int getIntegerNullToZero(String value) {
        if (StringUtils.isBlank(value)) {
            return 0;
        }
        return Integer.parseInt(value);
    }

    /**
     * 获取长整型 为空则返回0
     *
     * @param value 值
     * @return 值
     */
    public static long getLongNullToZero(String value) {
        if (StringUtils.isBlank(value)) {
            return 0;
        }

        return Long.parseLong(value);
    }

    /**
     * 获取单精度浮点型 为空则返回0
     *
     * @param value 值
     * @return 值
     */
    public static float getFloatNullToZero(String value) {
        if (StringUtils.isBlank(value)) {
            return 0;
        }
        return Float.parseFloat(value);
    }

    /**
     * 获取双精度浮点型 为空则返回0
     *
     * @param value 值
     * @return 值
     */
    public static double getDoubleNullToZero(String value) {
        if (StringUtils.isBlank(value)) {
            return 0;
        }
        return Double.parseDouble(value);
    }

    /**
     * 获取bigDecimal 如果为空则返回
     *
     * @param value 值
     * @return 值
     */
    public static BigDecimal getBigDecimalNullToZero(BigDecimal value) {
        return Optional.ofNullable(value).orElse(BigDecimal.ZERO);
    }

    public static boolean isNullOrZero(BigDecimal value) {
        return value == null || BigDecimal.ZERO.equals(value);
    }

    /*public static String numberToChinese(String str) {
        if(StringUtils.isBlank(str)){
            throw new SystemException("输入的数字为空");
        }

        String spot = "\\.";
        int parts = 2;
        String[] numbers = str.split(spot);
        if(numbers.length > parts){
            throw new BusinessException("请输入正确的数字类型", String.format("包含多个小数点：%s", str));
        }

        if(numbers.length == parts){
            String intNumber = numbers[0];
            String floatNumber = numbers[1];
        }else{

        }


        String result = "";



        int n = str.length();
        for (int i = 0; i < n; i++) {
            int num = str.charAt(i) - '0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        return result;
    }

    private String intToChinese(String str){
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
        if(str.length() > s2.length + 1){
            throw new BusinessException("数字超大");
        }
        int length = str.length();
        String result = "";
        for (int i = 0; i < length; i++) {
            int num = str.charAt(i) - '0';
            if(num < 0 || num > 9){
                throw new BusinessException("请输入正确的数字类型");
            }
            if()

            if(i == length - 1){
                result +=
            }
            if(i != length - 1 && num != 0){
                result += s1[num] + s2[length - i -1];
            }
        }
    }

    private String floatToChinese(String str){

    }*/
    //随机生成12位数
    public static String getString() {
        int[] redBall = new int[33];
        Random random = new Random();
        for (int i = 0; i < redBall.length; i++) {
            redBall[i] = i + 1;
        }

        int[] redNumber = new int[6];     //存储六个随机数的实际数组

        int index = -1;                             //通过随机数字数组下标获取随机数

        for (int i = 0; i < redNumber.length; i++) {

            index = random.nextInt(redBall.length - i);   //每次获取数字数组长度-i的随机数，比如第一次循环为33第二次为32，

            redNumber[i] = redBall[index];                   //把数字数组随机下标的值赋给实际数组

            int temp = redBall[index];                           //定义一个变量暂存下标为index时的值

            redBall[index] = redBall[redBall.length - 1 - i];  //把下标为index的值与数组下标最后的值交换

            redBall[redBall.length - 1 - i] = temp;             //交换后，下次循环把数字数组最后的值去掉，从而实现不重复

        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<redNumber.length;i++){
            int i1 = redNumber[i];
            if (i1<10){
                sb.append(0).append(i1);
            }else {
                sb.append(i1);
            }

        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String string = NumberUtil.getString();
        System.out.println("生成的随机数是："+string);
    }
}
